import axios from 'axios';
import query from 'query-string';

const BASE_URL = '/promotion/seckill';


export interface SeckillParam {
    name?: string;
    status?: number;
    page?: number;
    size?: number;
    sort?: Array<string>;
}

export interface SeckillResponse {
    id?: number;
    name: string;
    descs?: string;
    startTime?: string;
    endTime?: string;
    img?: string;
    status?: number;

    createdBy?: string;
    updatedBy?: string;
    createdAt?: string;
    updatedAt?: string;
}

export interface SeckillSaveCommand {
    id?: number;
    name: string;
    descs?: string;
    startTime?: string;
    endTime?: string;
    img?: string;
    status?: number;
}

export interface SeckillResponseList {
    list: SeckillResponse[];
    total: number;
}


// 通过SeckillParam参数获取分页列表
export function pageSeckill(params: SeckillParam) {
    return axios.get<SeckillResponseList>(`${BASE_URL}/page`, {
        params, paramsSerializer: (obj) => {
            return query.stringify(obj);
        }
    })
}

// 通过id获取秒杀活动信息
export function getSeckill(id: number) {
    return axios.get<SeckillResponse>(`${BASE_URL}/${id}`);
    
}

export function listSeckill(params: SeckillParam) {
    return axios.get<SeckillResponse[]>(`${BASE_URL}/list`, {
        params, paramsSerializer: (obj) => {
            return query.stringify(obj);
        }
    });
}


// 添加秒杀活动
export function addSeckill(params: SeckillSaveCommand) {
    return axios.post(`${BASE_URL}`, params);
}

// 更新秒杀活动
export function updateSeckill(params: SeckillSaveCommand) {
    return axios.put(`${BASE_URL}`, params);
}

// 批量删除秒杀活动
export function deleteSeckill(ids: number | Array<number>) {
    return axios.delete(`${BASE_URL}/${ids}`);
}

// 切换秒杀活动状态
export function switchSeckillStatus(id: number) {
    return axios.patch(`${BASE_URL}/switch/status/${id}`);
}